<?php
include 'header.php';
include 'constant.php';
include 'db_cred.php';
?>
<script type=text/javascript>
function validate_input()
{
  var event_name = document.forms["main"]["event_name"].value;
  if (event_name == "")
  {
    alert ("Error : Event Name must not be empty");
    return false;
  }

  var event_dt = document.forms["main"]["event_dt"].value;
  var regexp = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$/;
  if (! regexp.test(event_dt))
  {
    alert ("Error : Event Date is not in the correct format");
    return false;
  }

  var event_reminder_interval = document.forms["main"]["event_reminder_interval"].value;
  if ((event_reminder_interval < 0) ||  (event_reminder_interval >= 60))
  {
    alert ("Error : Reminder must be less than 60 minutes");
    return false;
  }

  var user_phone = document.forms["main"]["user_phone"].value;
  var regexp = /^\d{10}\s$/;
  if (! regexp.test(user_phone))
  {
    alert ("Error : Phone Number must contain 10 digits only");
    return false;
  } 
}
</script>
<?php
if ($_SESSION['authorized_user'] == '')
{
  include 'constant.php';
  print $CONST_ERR_INCORRECT_LOGIN_CRED . "<BR>\n";
  print "<A HREF=\"/\">Back</A>\n";
}
else
{
  # if event_name is not null => trying to update an event detail
  if ($_REQUEST['event_name'] != '')
  {
       $_SESSION['event_update_status'] = 'Updated event ' . $_REQUEST['event_name'];
       $query = sprintf ("update event set event_name = '%s', event_dt = '%s' - interval %s hour, event_reminder_dt = '%s' - INTERVAL %s MINUTE, alert_recipients = '%s',
                        event_is_muted = '%s' where event_id = '%s' and user_name = '%s'",
                     $_REQUEST['event_name'],
                     $_REQUEST['event_dt'], $_SESSION['user_timezone_offset'],
                     $_REQUEST['event_dt'], ($_SESSION['user_timezone_offset'] * 60) + $_REQUEST['event_reminder_interval'],
                     $_REQUEST['alert_recipients'],
                     $_REQUEST['event_is_muted'],
                     $_REQUEST['event_id'],
                     $_SESSION['authorized_user']
       );
       $result = mysql_query($query) or ($_SESSION['event_update_status'] = 'Error : ' . mysql_error());
  }

  # display event detail
  $query = "select event_name, (event_dt + interval " . $_SESSION['user_timezone_offset'] . " hour) event_dt, minute(timediff(event_dt, event_reminder_dt)) event_reminder_interval, alert_recipients, event_is_muted from event where user_name = '". $_SESSION['authorized_user'] . "' and event_id = '" . $_REQUEST['event_id'] . "'";

  $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  $row = mysql_fetch_assoc($result);
  printf("<FORM NAME=\"main\" ONSUBMIT=\"return validate_input()\">\n");
  printf("<INPUT TYPE=\"hidden\" NAME=\"event_id\" VALUE=\"" . $_REQUEST['event_id'] . "\" />\n");
  printf("<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>\n");
  printf("<TR><TH>%s</TH><TD WIDTH=5 /><TD><INPUT TYPE=TEXT WIDTH=30 NAME=%s VALUE=\"%s\"></TD></TR>\n","Event Name","event_name",$row["event_name"]);
  printf("<TR><TH>%s</TH><TD WIDTH=5 /><TD><INPUT TYPE=TEXT WIDTH=30 NAME=%s VALUE=\"%s\"></TD></TR>\n","Event Date (YYYY-MM-DD HH24:MI:SS)","event_dt",$row["event_dt"]);
  printf("<TR><TH>%s</TH><TD WIDTH=5 /><TD><INPUT TYPE=TEXT WIDTH=30 NAME=%s VALUE=\"%s\"></TD></TR>\n","Reminder (minutes)","event_reminder_interval",$row["event_reminder_interval"]);
  printf("<TR><TH>%s</TH><TD WIDTH=5 /><TD><INPUT TYPE=TEXT WIDTH=30 NAME=%s VALUE=\"%s\"></TD></TR>\n","Running Late SMS Recipients","alert_recipients",$row["alert_recipients"]);
  printf("<TR><TH>%s</TH><TD WIDTH=5 /><TD><INPUT TYPE=RADIO NAME=%s VALUE=\"%s\"%s> On </INPUT><INPUT TYPE=RADIO NAME=%s VALUE=\"%s\"%s> Off </INPUT></TD></TR>\n","Alert","event_is_muted","0",($row["event_is_muted"] ? "" : " CHECKED"),"event_is_muted","1",$row["event_is_muted"] ? " CHECKED" : "");
  printf("<TR><TD COLSPAN=3><FONT COLOR=\"RED\">%s</FONT></TD></TR>\n",$_SESSION['event_update_status']);
  printf("<TR><TD COLSPAN=3><INPUT TYPE=SUBMIT VALUE=\"Update\"> <INPUT TYPE=BUTTON ONCLICK=\"location.href='/'\" VALUE=\"Go to Main Page\" /></TD></TR>\n");
  printf("</TABLE>\n");
  printf("</FORM>\n");

}
include 'footer.php';
?>